/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using System.IO;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.MainMDI
{
    sealed class FolderTreeBuilder
    {
        public static void BuildFolderTree(MyPhotoIndex.Controls.TreeView treeView, IImageCollection imageCollection)
        {
            List<String> dirList = new List<String>();
            Dictionary<String, int> dirMap = new Dictionary<string, int>();

            foreach (IImageData imageData in imageCollection)
            {
                String dirName = Path.GetDirectoryName(imageData.FileName);
                if (dirMap.ContainsKey(dirName) == false)
                {
                    dirList.Add(dirName);
                    dirMap.Add(dirName, 0);
                }
            }

            dirList.Sort();
            treeView.Nodes.Clear();
            BuildFolderTree(treeView.Nodes, dirList, 0);
        }

        private static void BuildFolderTree(TreeNodeCollection root, List<String> dirList, int level)
        {
            if (dirList.Count == 0)
            {
                return;
            }

            String lastDir = String.Empty;
            List<String> newList = new List<String>();

            foreach (String dirName in dirList)
            {
                String[] paths = dirName.Split(new char[] { Path.DirectorySeparatorChar });
                if (paths.Length <= level)
                {
                    continue;
                }

                String baseDir = paths[level];

                if (string.Compare(lastDir,  baseDir, true) != 0 &&
                    string.IsNullOrEmpty(lastDir) == false)
                {
                    TreeNode node = new TreeNode(lastDir);

                    BuildFolderTree(node.Nodes, newList, level + 1);

                    root.Add(node);
                    newList.Clear();
                }

                newList.Add(dirName);
                lastDir = baseDir;
            }

            if (String.IsNullOrEmpty(lastDir) == false)
            {
                TreeNode newNode = new TreeNode(lastDir);
                BuildFolderTree(newNode.Nodes, newList, level + 1);
                root.Add(newNode);
            }
        }
    }
}
